package com.getjsp.util;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.os.Environment;

/** 
* @ClassName: FileUtils 
* @Description: TODO(存储数据到SD卡工具类) 
* @author lizh,henku126_163.com 
* @date 2013-12-11 下午9:29:32 
*  
*/ 
public class FileUtils {
	private String SDPATH;

	public String getSDPATH() {
		return SDPATH;
	}
	public FileUtils() {
		/*得到外部存储设备目录
		  /SDCARD
		 */
		SDPATH = Environment.getExternalStorageDirectory() + "/";
	}
	/*在SD卡上创建文件*/
	public File creatSDFile(String fileName) throws IOException {
		File file = new File(SDPATH + fileName);
		file.createNewFile();
		return file;
	}
	
	/*在SD卡上创建目录*/
	public File creatSDDir(String dirName) {
		File dir = new File(SDPATH + dirName);
		dir.mkdirs();
		return dir;
	}

	//判断SD卡上的文件上否存
	public boolean isFileExist(String fileName){
		File file = new File(SDPATH + fileName);
		return file.exists();
	}
	
	//将一个InputStream写入文件�?/
	public File write2SDFromInput(String path,String fileName,InputStream input){
		File file = null;
		OutputStream output = null;
		try{
			creatSDDir(path);
			file = creatSDFile(path + fileName);
			output = new FileOutputStream(file);
			byte buffer [] = new byte[4 * 1024];
			while((input.read(buffer)) != -1){
				output.write(buffer);
			}
			output.flush();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			try{
				output.close();
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}
		return file;
	}
	
	  
	
	 //获得文件夹或者文件的大小，如果要获得磁盘大小用jconfig效率更高    
	 public static Long getSize(String strName) {  
		 
		 Long TotalSize = 0L;  
	      File f = new File(strName);  
	        if (f.isFile())  
	            return f.length(); 	        
	        else {  
	            if (f.isDirectory()) {  
	                File[] contents = f.listFiles();  
	                for (int i = 0; i < contents.length; i++) {  
	                    if (contents[i].isFile())  
	                        TotalSize += contents[i].length();  
	                    else {  
	                        if (contents[i].isDirectory())  
	                            TotalSize += getSize(contents[i].getPath());  
	                    }  
	                }  
	            }  
	        }  
	        return TotalSize;  
	   
	    }
}
